package com.cooba.activitys.setting;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.application.MyApplication;
import com.cooba.utils.Constants;
import com.cooba.utils.DataCleanManager;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by Administrator on 2015/12/15.
 * 设置
 */
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mMe_image_back;
    private RelativeLayout mDataClean_rl;
    private RelativeLayout mSysMsg_rl;
    private RelativeLayout mFeedBack_rl;
    private ToggleButton mToggleBtn;
    private Button mBt_exit;
    private String login_id;
    private Context mContext;
    private String uuid;
    private String push_set;
    private String cacheDataSize = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        PushAgent.getInstance(this).onAppStart();

        mContext = this;
        mMe_image_back = (ImageView) findViewById(R.id.set_image_back);
        mDataClean_rl = (RelativeLayout) findViewById(R.id.clearinfo_rl);
        mFeedBack_rl = (RelativeLayout) findViewById(R.id.feedback_rl);
        mToggleBtn = (ToggleButton) findViewById(R.id.push_setting);
        mBt_exit = (Button) findViewById(R.id.bt_exit);
        mMe_image_back.setOnClickListener(this);
        mBt_exit.setOnClickListener(this);
        mToggleBtn.setOnClickListener(this);
        mDataClean_rl.setOnClickListener(this);
        mFeedBack_rl.setOnClickListener(this);
        login_id = SharedPreferencesUtil.getData(mContext, "login_id", "");
        uuid = SharedPreferencesUtil.getData(mContext, "uuid", "");
        push_set = SharedPreferencesUtil.getData(mContext, "push_set", "1");
        if (!TextUtils.isEmpty(login_id)) {
            mBt_exit.setVisibility(View.VISIBLE);
        } else if (!TextUtils.isEmpty(uuid)) {
            mBt_exit.setVisibility(View.VISIBLE);
        }
        if (push_set.equals("0")) {
            mToggleBtn.setChecked(false);
        } else {
            mToggleBtn.setChecked(true);
        }


    }

    @Override
    public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
            case R.id.set_image_back:
//                intent = new Intent(getApplication(), MainActivity.class);
//                startActivity(intent);
                finish();
                break;
            case R.id.clearinfo_rl:
                final MyApplication application=MyApplication.getMyApplication();
                if (!application.getIsCanClean()){
                    Toast.makeText(mContext,"您已经清理过了,重启App后可再次清理!",Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    cacheDataSize=DataCleanManager.getTotalCacheSize(mContext);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("是否清理缓存?").setMessage("当前缓存:" + cacheDataSize).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DataCleanManager.clearAllCache(mContext);
                        Toast.makeText(mContext,"清理成功!",Toast.LENGTH_SHORT).show();
                        application.setIsCanClean(false);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
                dialog.show();
                break;
            case R.id.push_setting:
                if (mToggleBtn.isChecked()) {
                    SharedPreferencesUtil.saveData(mContext, "push_set", "1");
                } else {
                    SharedPreferencesUtil.saveData(mContext, "push_set", "0");
                }
                break;
            case R.id.feedback_rl:
                intent = new Intent(getApplication(), FeedBackActivity.class);
                startActivity(intent);
                break;
            //安全退出
            case R.id.bt_exit:
                login_id = SharedPreferencesUtil.getData(mContext, "login_id", "");
                if (!TextUtils.isEmpty(login_id)) {
                    ShareSDK.initSDK(mContext);
                    Platform plat_qq = ShareSDK.getPlatform(SettingActivity.this,
                            QZone.NAME);
                    Platform plat_wechat = ShareSDK.getPlatform(SettingActivity.this,
                            Wechat.NAME);
                    Platform plat_sina = ShareSDK.getPlatform(SettingActivity.this,
                            SinaWeibo.NAME);
                    plat_qq.removeAccount();
                    plat_wechat.removeAccount();
                    plat_sina.removeAccount();
                    SharedPreferencesUtil.saveData(mContext,"uuid","");
                    SharedPreferencesUtil.saveData(mContext, "uuid_url", "");
                }
                if (!TextUtils.isEmpty(login_id)) {
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.addBodyParameter("phone", login_id);
                    httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.Logout, params, new RequestCallBack<String>() {
                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            String status = null;
                            try {
                                status = JSON.parseObject(responseInfo.result).getString("status");
                                if (status.equals("0")) {
                                    Toast.makeText(getApplication(), "退出失败", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getApplication(), "退出成功", Toast.LENGTH_SHORT).show();
                                    SharedPreferencesUtil.saveData(mContext, "login_id", "");
                                    SharedPreferencesUtil.saveData(mContext, "password", "");
                                    SharedPreferencesUtil.saveData(mContext, "name", "");
                                    SharedPreferencesUtil.saveData(mContext,"login_state","0");
                                    //SharedPreferencesUtil.saveData(mContext, "allTotalIntegral", "");
                                    Intent intent = new Intent(Constants.LOGOUT_ACTION);
                                    sendBroadcast(intent);
                                    finish();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                        }

                    });
                }
                break;


        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
